package com.rxh.service.impl;


import com.rxh.mapper.core.*;
import com.rxh.mapper.finance.FinanceBusinessMoneyMapper;
import com.rxh.mapper.finance.FinanceDrawingMapper;
import com.rxh.mapper.merchant.MerchantInfoMapper;
import com.rxh.mapper.merchant.MerchantWebSiteMapper;
import com.rxh.mapper.sys.SysConstantMapper;
import com.rxh.pojo.Result;
import com.rxh.pojo.core.*;
import com.rxh.pojo.finance.FinanceBusinessMoney;
import com.rxh.pojo.payment.Refund;
import com.rxh.service.OperationService;
import com.rxh.utils.GlobalConfiguration;
import com.rxh.utils.HttpClientUtils;
import com.rxh.utils.JsonUtils;
import com.rxh.utils.SystemConstant;
import com.rxh.vo.VoAbnormal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 王德明
 * Date: 2018/5/16
 * Time: 10:59
 * Project: Management_new
 * Package: com.rxh.service.impl
 */
@Service
public class OperationServiceImpl implements OperationService {

    @Resource
    private GlobalConfiguration globalConfiguration;

    @Resource
    private CoreOrderChangeMapper coreOrderChangeMapper;

    @Resource
    private FinanceDrawingMapper financeDrawingMapper;

    @Resource
    private MerchantInfoMapper merchantInfoMapper;

    @Resource
    private MerchantWebSiteMapper merchantWebSiteMapper;

    @Resource
    private SysConstantMapper constantMapper;

    @Resource
    private CoreOrderMapper coreOrderMapper;

    @Resource
    private CoreMerchantOrderMapper coreMerchantOrderMapper;

    @Resource
    private CoreMerchantOrderRmbMapper coreMerchantOrderRmbMapper;

    @Resource
    private FinanceBusinessMoneyMapper financeBusinessMoneyMapper;

    @Resource
    private CoreAcquirerAccountMapper acquirerAccountMapper;

    @Resource
    private CoreAcquirerMapper coreAcquirerMapper;

    @Autowired
    private ChannelOrderInfoMapper channelOrderInfoMapper;
    /**
     * 获取下拉框参数
     */
    @Override
    public Map<String, Object> getInit() {
        Map<String, Object> mapParam = new HashMap<String, Object>();
        mapParam.put("merchant", merchantInfoMapper.selectAllIdAndName());      //商户信息
        mapParam.put("siteurl", merchantWebSiteMapper.selectAllIdAndMerIdAndWeb());    //域名
        mapParam.put("exceptionstatus", constantMapper.selectByGroupName(SystemConstant.EXCEPTION_STATUS)); //异常状态
        return mapParam;
    }

    /**
     * 获取全部数据
     */
    @Override
    public List<VoAbnormal> getcoreOrderChangeInfo(Map<String, Object> paramMap) {
        VoAbnormal voAbnormal = new VoAbnormal();
        List<VoAbnormal> voAbnormalList = coreOrderChangeMapper.getcoreOrderChangeInfo(paramMap);
        int result = coreOrderChangeMapper.getAbnormalCheckNum();
        voAbnormal.setResult(result);
        voAbnormalList.add(voAbnormal);
        return voAbnormalList;
    }

    /**
     * 查询数据
     */
    @Override
    public List<VoAbnormal> getAbnormalCheck(Map<String, Object> paramMap) {
        return coreOrderChangeMapper.getAbnormalCheck(paramMap);
    }

    @Override
    public Result updateAbnormalAudit(VoAbnormal voAbnormal) {
        Result<CoreOrderChange> result = new Result<>();
        if (voAbnormal.getStatus() == CoreOrderChange.STATUS_SUCCESS) {
            //根据OrderId获取CoreOrder对象
            CoreOrder coreOrder = coreOrderMapper.selectByPrimaryKey(voAbnormal.getOrderId());
            //根据id获取CoreMerchantOrder对象
            CoreMerchantOrder coreMerchantOrder = coreMerchantOrderMapper.selectByOrderId(coreOrder.getId());
            //根据id获取CoreMerchantOrderRMB对象
            CoreMerchantOrderRmb coreMerchantOrderRmb = coreMerchantOrderRmbMapper.getALLCoreMerchantOrderRmbById(coreOrder.getId());
            //根据orderID获取channelOrderInfo对象
            ChannelOrderInfo channelOrderInfo = channelOrderInfoMapper.selectByOrderId(voAbnormal.getOrderId());
            if (voAbnormal.getType() == CoreOrderChange.TYPE_FULL_REFUND || voAbnormal.getType() == CoreOrderChange.TYPE_PARTIAL_REFUND) {
                result = refund(voAbnormal, coreOrder, channelOrderInfo);
                if (result.getCode() == Result.SUCCESS) {
                    CoreOrderChange orderChange = result.getData();
                    voAbnormal.setStatus(orderChange.getStatus());
                    voAbnormal.setExceptionBankId(orderChange.getExceptionBankId());
                    voAbnormal.setRemark(orderChange.getRemark());
                    switch (orderChange.getStatus()) {
                        // 退款成功
                        case CoreOrderChange.STATUS_SUCCESS:
                            // 更新CoreMerchantOrder退款金额
                            coreMerchantOrder.setSourceRefund(null == coreMerchantOrder.getSourceRefund() ? voAbnormal.getAmount() : voAbnormal.getAmount().add(coreMerchantOrder.getSourceRefund()));
                            // 更新channal_order_info的部分退款*coreOrder手续费，全部退款
                            if (voAbnormal.getType() == CoreOrderChange.TYPE_FULL_REFUND) {
                                channelOrderInfo.setChannelRefundAmount(coreMerchantOrder.getSourceRefund().multiply(coreOrder.getRate()));
                            } else {
                                channelOrderInfo.setChannelRefundAmount(channelOrderInfo.getChannelAmount());
                            }
                            coreMerchantOrderMapper.updateByPrimaryKeySelective(coreMerchantOrder);
                            channelOrderInfoMapper.updateByPrimaryKeySelective(channelOrderInfo);
                            // 更新CoreMerchantOrderRMB退款金额
                            coreMerchantOrderRmb.setSourceRmbRefund(null == coreMerchantOrderRmb.getSourceRmbRefund() ? voAbnormal.getRmbAmount() : voAbnormal.getRmbAmount().add(coreMerchantOrderRmb.getSourceRmbRefund()));
                            coreMerchantOrderRmbMapper.updateByPrimaryKeySelective(coreMerchantOrderRmb);
                            // 更新CoreOrder订单表相关数据
                            coreOrder.setExceptionStatus(voAbnormal.getType());
                            coreOrder.setShipStatus(SystemConstant.SHIP_STATUS_SHIPPED);
                            coreOrder.setExceptionTime(voAbnormal.getAuditTime());
                            coreOrder.setExceptionAmount(coreMerchantOrder.getSourceRefund());
                            if (!updateBusinessMoneyTemp(coreOrder, voAbnormal)) {
                                result.setCode(Result.FAIL);
                                result.setMsg("退款审核失败，商户余额对象不存在或更新失败！");
                            } else {
                                coreOrderMapper.updateByPrimaryKeySelective(coreOrder);
                            }
                            break;
                        // 银行拒绝退款
                        case CoreOrderChange.STATUS_FAIL:
                            break;
                        // 退款申请成功(银行处理退款中)
                        case CoreOrderChange.STATUS_PROCESSING:
                            coreOrder.setExceptionStatus(CoreOrderChange.TYPE_BANK_PROCESSING_REFUND);
                            if (!updateBusinessMoneyTemp(coreOrder, voAbnormal)) {
                                result.setCode(Result.FAIL);
                                result.setMsg("退款申请失败，商户余额对象不存在或更新失败！");
                            } else {
                                coreOrderMapper.updateByPrimaryKeySelective(coreOrder);
                            }
                        default:
                            break;
                    }
                } else {
                    return result;
                }
            }
            if (voAbnormal.getType() == CoreOrderChange.TYPE_FULL_REFUSED || voAbnormal.getType() == CoreOrderChange.TYPE_PARTIAL_REFUSED) {
                //core_order
                coreOrder.setExceptionStatus(voAbnormal.getType());
                coreOrder.setShipStatus(SystemConstant.SHIP_STATUS_SHIPPED);
                coreOrder.setExceptionTime(voAbnormal.getAuditTime());
                coreOrder.setExceptionAmount(null == coreOrder.getExceptionAmount() ? voAbnormal.getAmount() : coreOrder.getExceptionAmount().add(voAbnormal.getAmount()));
                //core_merchant_order
                coreMerchantOrder.setSourceProtest(null == coreMerchantOrder.getSourceProtest() ? voAbnormal.getAmount() : voAbnormal.getAmount().add(coreMerchantOrder.getSourceProtest()));
                // 更新channal_order_info的部分退款*coreOrder手续费，全部退款
                if (voAbnormal.getType() == CoreOrderChange.TYPE_PARTIAL_REFUSED) {
                    channelOrderInfo.setChannelProtestAmount(coreMerchantOrder.getSourceProtest().multiply(coreOrder.getRate()));
                } else {
                    channelOrderInfo.setChannelProtestAmount(channelOrderInfo.getChannelAmount());
                }
                coreMerchantOrder.setSourceProtestCharge(null == coreMerchantOrder.getSourceProtestCharge() ? voAbnormal.getFee() : voAbnormal.getFee().add(coreMerchantOrder.getSourceProtestCharge()));
                //core_merchant_order_rmb
                coreMerchantOrderRmb.setSourceRmbProtestCharge(null == coreMerchantOrderRmb.getSourceRmbProtestCharge() ? voAbnormal.getRmbFee() : voAbnormal.getRmbFee().add(coreMerchantOrderRmb.getSourceRmbProtestCharge()));
                coreMerchantOrderRmb.setSourceRmbProtest(null == coreMerchantOrderRmb.getSourceRmbProtest() ? voAbnormal.getRmbAmount() : voAbnormal.getRmbAmount().add(coreMerchantOrderRmb.getSourceRmbProtest()));
                if (!updateBusinessMoneyTemp(coreOrder, voAbnormal)) {
                    result.setCode(Result.FAIL);
                    result.setMsg("拒付审核失败，商户余额对象不存在或更新失败！");
                } else {
                    coreOrderMapper.updateByPrimaryKeySelective(coreOrder);
                    coreMerchantOrderMapper.updateByPrimaryKeySelective(coreMerchantOrder);
                    coreMerchantOrderRmbMapper.updateByPrimaryKeySelective(coreMerchantOrderRmb);
                    channelOrderInfoMapper.updateByPrimaryKeySelective(channelOrderInfo);
                    result.setCode(Result.SUCCESS);
                    result.setMsg("拒付审核成功！");
                }
            }
        } else {
            result.setCode(Result.SUCCESS);
            result.setMsg("审核未通过");
        }
        coreOrderChangeMapper.updateByPrimaryKeySelective(voAbnormal);
        return result;
    }

    public Result<CoreOrderChange> refund(VoAbnormal voAbnormal, CoreOrder coreOrder, ChannelOrderInfo channelOrderInfo) {
        Refund refund = new Refund();
        refund.setCoreOrderChange(voAbnormal);
        refund.setOrder(coreOrder);
        CoreAcquirer coreAcquirer = coreAcquirerMapper.selectByPrimaryKey(acquirerAccountMapper.selectByPrimaryKey(coreOrder.getAccountId()).getAcquirerId());
        refund.setCoreAcquirer(coreAcquirer);
        refund.setChannelOrderInfo(channelOrderInfo);
        String url = globalConfiguration.getPaymentUrl() + "/refund";
        // String url = "http://localhost:8090" + "/refund";
        return JsonUtils.jsonToPojo(HttpClientUtils.doPostJson(HttpClientUtils.getHttpClient(), url, JsonUtils.objectToJson(refund)), Result.class, CoreOrderChange.class);
    }

    /**
     * 更新finance_business_money表
     *
     * @param coreOrder
     * @param voAbnormal
     * @return
     */
    public Boolean updateBusinessMoneyTemp(CoreOrder coreOrder, VoAbnormal voAbnormal) {
        FinanceBusinessMoney financeBusinessMoney = financeBusinessMoneyMapper.getFinanceBusinessMoneyByRefId(coreOrder.getMerId());
        if (financeBusinessMoney != null) {
            financeBusinessMoney.setBookBalance(financeBusinessMoney.getBookBalance().subtract(voAbnormal.getRmbAmount()).setScale(2, BigDecimal.ROUND_UP));
            if (voAbnormal.getType() == SystemConstant.FULL_REFUND || voAbnormal.getType() == SystemConstant.PARTIAL_REFUND) {
                financeBusinessMoney.setUsableAmount(financeBusinessMoney.getUsableAmount().subtract(voAbnormal.getRmbAmount()).setScale(2, BigDecimal.ROUND_UP));
                financeBusinessMoney.setRefundAmount(financeBusinessMoney.getRefundAmount().add(voAbnormal.getRmbAmount()).setScale(2, BigDecimal.ROUND_UP));
            } else if (voAbnormal.getType() == SystemConstant.FULL_REFUSED || voAbnormal.getType() == SystemConstant.PARTIAL_REFUSED) {
                financeBusinessMoney.setUsableAmount(financeBusinessMoney.getUsableAmount().subtract(voAbnormal.getRmbAmount()).subtract(voAbnormal.getRmbFee()).setScale(2, BigDecimal.ROUND_UP));
                financeBusinessMoney.setProtestAmount(financeBusinessMoney.getProtestAmount().add(voAbnormal.getRmbAmount()).setScale(2, BigDecimal.ROUND_UP));
                financeBusinessMoney.setProtestChargeAmount(financeBusinessMoney.getProtestChargeAmount().add(voAbnormal.getRmbFee()).setScale(2, BigDecimal.ROUND_UP));
            }
            return financeBusinessMoneyMapper.updateFinanceBusinessMoneyByPrimaryKeySelective(financeBusinessMoney) != 0;
        }
        return false;
    }


    /**
     * 审核数据
     */

    // public String getAbnormalAudit(CoreOrderChange coreOrderChange) throws IOException {
    //     String msg = "1";
    //     Map<String, Object> orderchangeMap = new HashMap<>();
    //     //根据order_id获取coreOrder对象
    //     CoreOrder coreOrder = coreOrderMapper.selectByOrderId(coreOrderChange.getOrderId().toString());
    //     //根据account_id获取coreAcquireAccount对象
    //     CoreAcquirerAccount coreAcquirerAccount = acquirerAccountMapper.selectByPrimaryKey(Long.parseLong(coreOrder.getAccountId().toString()));
    //     //根据order_id获取coreOrderChange对象
    //     CoreOrderChange coreOrderChangeAll = coreOrderChangeMapper.selectOrderChangeByOrderId(Long.parseLong(coreOrderChange.getOrderId().toString()));
    //     if (coreOrderChange.getStatus() == 1) {
    //         //exception_amount累加
    //         BigDecimal orderamount = coreOrder.getExceptionAmount();
    //         BigDecimal paramorderamount = coreOrderChange.getAmount();
    //         BigDecimal exception_amount = orderamount.add(paramorderamount);
    //         //处理费获取(拒付处理费和退款处理费)
    //         BigDecimal source_protest_charge = coreOrderChange.getFee();
    //         //交易汇率获取
    //         BigDecimal change_rate = coreOrderChange.getChangeRate();
    //         //通道交易汇率获取
    //         BigDecimal change_rate_channel = (BigDecimal) channelOrderInfoMapper.getOrderInfo(coreOrderChange.getOrderId().toString()).get("channel_trans_rate");
    //         //core_merchant_order_rmb的rmb拒付金额
    //         BigDecimal change_rate_rmb = exception_amount.multiply(change_rate);
    //         //channel_order_info通道拒付金额
    //         BigDecimal change_rate_rmb_channel = exception_amount.multiply(change_rate_channel);
    //         //core_order_change的rmb_fee获取
    //         BigDecimal fee_rmb = change_rate.multiply(source_protest_charge);
    //         //获取finance_business_money的usable_amount
    //         BigDecimal usableAmount = (BigDecimal) financeBusinessMoneyMapper.getUsableAmount(Integer.parseInt(coreOrder.getMerId().toString())).get("usable_amount");
    //         //获取finance_business_money的protest_charge_amount
    //         BigDecimal protest_charge_amount = (BigDecimal) financeBusinessMoneyMapper.getUsableAmount(Integer.parseInt(coreOrder.getMerId().toString())).get("protest_charge_amount");
    //         //获取finance_business_money的protest_amount
    //         BigDecimal protest_amount = (BigDecimal) financeBusinessMoneyMapper.getUsableAmount(Integer.parseInt(coreOrder.getMerId().toString())).get("protest_amount");
    //         //获取finance_business_money的refund_amount
    //         BigDecimal refund_amount = (BigDecimal) financeBusinessMoneyMapper.getUsableAmount(Integer.parseInt(coreOrder.getMerId().toString())).get("refund_amount");
    //         //finance_business_money的拒付总金额累加
    //         BigDecimal protest_amount_total = protest_amount.add(change_rate_rmb);
    //         //finance_business_money的处理费累加
    //         BigDecimal protest_charge_amount_total = protest_charge_amount.add(fee_rmb);
    //         //finance_business_money的可用金额减去拒付总金额累加和处理费的累加
    //         BigDecimal usableAmountTotal = usableAmount.subtract(change_rate_rmb).subtract(fee_rmb);
    //         // Map<String, Object> orderMap = new HashMap<>();
    //         // Map<String, Object> orderChantMap = new HashMap<>();
    //         // Map<String, Object> orderChantRMBMap = new HashMap<>();
    //         // Map<String, Object> orderInfoMap = new HashMap<>();
    //         // Map<String, Object> orderBusinessMoneyMap = new HashMap<>();
    //         switch (coreOrderChange.getType()) {
    //             //正常
    //             case 0:
    //                 coreOrder.setExceptionTime(new Date());
    //                 coreOrder.setExceptionStatus((short) 0);
    //                 break;
    //             //冻结
    //             case 1:
    //                 coreOrder.setExceptionTime(new Date());
    //                 coreOrder.setExceptionStatus((short) 1);
    //                 break;
    //             //全部退款
    //             case 2:
    //                 coreOrder.setExceptionTime(new Date());
    //                 coreOrder.setExceptionStatus((short) 2);
    //                 coreOrder.setExceptionAmount(coreOrderChange.getAmount());
    //                 coreOrder.setId(coreOrderChange.getOrderId());
    //                 if (!coreOrder.getPayMode().trim().equalsIgnoreCase("Credit")) {
    //                     msg = refund(coreOrderChange, coreOrder);
    //                 }
    //                 if (coreAcquirerAccount.getName().equalsIgnoreCase("AllinPay") || coreAcquirerAccount.getName().equalsIgnoreCase("Grepay")) {
    //                     msg = refund(coreOrderChange, coreOrder);
    //                 }
    //                 if ("1".equals(msg) && (!coreAcquirerAccount.getName().equalsIgnoreCase("AllinPay") || !coreAcquirerAccount.getName().equalsIgnoreCase("GrePay"))) {
    //                     coreOrderChangeAll.setStatus(coreOrderChange.getStatus());
    //                     coreOrderChangeAll.setRemark(coreOrderChange.getRemark());
    //                     coreOrderChangeAll.setAuditor(coreOrderChange.getAuditor());
    //                     coreOrderChangeAll.setAuditTime(new Date());
    //                     coreOrderChangeAll.setId(coreOrderChange.getOrderId());
    //                     //更新core_merchant_order表的数据
    //                     orderChantMap.put("source_refund", paramMap.get("exception_amount"));
    //                     orderChantMap.put("source_refund_charge", source_protest_charge);
    //                     orderChantMap.put("order_id", paramMap.get("order_id"));
    //                     //更新core_merchant_order_rmb表的数据
    //                     orderChantRMBMap.put("source_rmb_protest", null);
    //                     orderChantRMBMap.put("source_rmb_protest_charge", null);
    //                     orderChantRMBMap.put("source_rmb_refund", new BigDecimal(paramMap.get("exception_amount").toString()).multiply(new BigDecimal(paramMap.get("change_rate").toString())));
    //                     orderChantRMBMap.put("source_rmb_refund_charge", new BigDecimal(paramMap.get("source_protest_charge").toString()).multiply(new BigDecimal(paramMap.get("change_rate").toString())));
    //                     orderChantRMBMap.put("order_id", paramMap.get("order_id"));
    //                     //更新channel_order_info表的数据
    //                     orderInfoMap.put("channel_refund_amount", new BigDecimal(paramMap.get("exception_amount").toString()).multiply(change_rate_channel));
    //                     orderInfoMap.put("channel_protest_amount", null);
    //                     orderInfoMap.put("order_id", paramMap.get("order_id"));
    //                     //更新finance_business_money表的数据
    //                     orderBusinessMoneyMap.put("refund_amount", refund_amount.add(new BigDecimal(orderChantRMBMap.get("source_rmb_refund").toString())));
    //                     orderBusinessMoneyMap.put("usable_amount", usableAmount.subtract(new BigDecimal(orderBusinessMoneyMap.get("refund_amount").toString())));
    //                     orderBusinessMoneyMap.put("protest_amount", null);
    //                     orderBusinessMoneyMap.put("protest_charge_amount", null);
    //                     orderBusinessMoneyMap.put("ref_id", paramMap.get("mer_id"));
    //                     coreOrderMapper.updateOrder(orderMap);
    //                     coreOrderChangeMapper.getAbnormalAudit(orderchangeMap);
    //                     coreMerchantOrderMapper.updatecoreOrder(orderChantMap);
    //                     coreMerchantOrderRmbMapper.updatecoreOrderRMB(orderChantRMBMap);
    //                     channelOrderInfoMapper.updateorderInfoProtest(orderInfoMap);
    //                     financeBusinessMoneyMapper.updateFinanceBusinessMoney(orderBusinessMoneyMap);
    //                     msg = "1";
    //                     break;
    //                 } else if ("2".equals(msg)) {
    //                     orderchangeMap.put("status", msg);
    //                     orderchangeMap.put("remark", paramMap.get("remark"));
    //                     orderchangeMap.put("order_id", coreOrderChange.getId());
    //                     orderchangeMap.put("auditor", paramMap.get("auditor"));
    //                     orderchangeMap.put("audit_time", new Date());
    //                     coreOrderMapper.updateOrder(orderMap);
    //                     coreOrderChangeMapper.getAbnormalAudit(orderchangeMap);
    //                     msg = "2";
    //                 } else if ("3".equals(msg)) {
    //                     orderchangeMap.put("auditor", paramMap.get("auditor"));
    //                     orderchangeMap.put("audit_time", new Date());
    //                     orderchangeMap.put("status", 3);
    //                     orderchangeMap.put("order_id", coreOrderChange.getId());
    //                     coreOrderMapper.updateOrder(orderMap);
    //                     coreOrderChangeMapper.getAbnormalAudit(orderchangeMap);
    //                     msg = "3";
    //                 } else if ("4".equals(msg)) {
    //                     orderMap.put("exception_time", new Date());
    //                     orderMap.put("exception_status", 7);
    //                     orderMap.put("exception_amount", null);
    //                     orderMap.put("order_id", paramMap.get("order_id"));
    //                     orderchangeMap.put("auditor", paramMap.get("auditor"));
    //                     orderchangeMap.put("audit_time", new Date());
    //                     orderchangeMap.put("status", 3);
    //                     orderchangeMap.put("order_id", coreOrderChange.getId());
    //                     orderBusinessMoneyMap.put("refund_amount", refund_amount.add(new BigDecimal(orderChantRMBMap.get("source_rmb_refund").toString())));
    //                     orderBusinessMoneyMap.put("usable_amount", usableAmount.subtract(new BigDecimal(orderBusinessMoneyMap.get("refund_amount").toString())));
    //                     orderBusinessMoneyMap.put("protest_amount", null);
    //                     orderBusinessMoneyMap.put("protest_charge_amount", null);
    //                     orderBusinessMoneyMap.put("ref_id", paramMap.get("mer_id"));
    //                     coreOrderMapper.updateOrder(orderMap);
    //                     coreOrderChangeMapper.getAbnormalAudit(orderchangeMap);
    //                     financeBusinessMoneyMapper.updateFinanceBusinessMoney(orderBusinessMoneyMap);
    //                     msg = "4";
    //                 }
    //                 break;
    //             //部分退款
    //             case 3:
    //                 //更新core_order表的数据
    //                 orderMap.put("exception_time", new Date());
    //                 orderMap.put("exception_status", 3);
    //                 orderMap.put("exception_amount", exception_amount);
    //                 orderMap.put("order_id", paramMap.get("order_id"));
    //                 if (!paramMap.get("pay_mode").toString().equalsIgnoreCase("Credit")) {
    //                     msg = refund(coreOrderChange, coreOrder);
    //                 }
    //                 if (coreAcquirerAccount.getName().equalsIgnoreCase("AllinPay") || coreAcquirerAccount.getName().equalsIgnoreCase("GrePay")) {
    //                     msg = refund(coreOrderChange, coreOrder);
    //                 }
    //                 if ("1".equals(msg) && (!coreAcquirerAccount.getName().equalsIgnoreCase("AllinPay") || !coreAcquirerAccount.getName().equalsIgnoreCase("GrePay"))) {
    //                     orderchangeMap.put("status", paramMap.get("status"));
    //                     orderchangeMap.put("remark", paramMap.get("remark"));
    //                     orderchangeMap.put("order_id", coreOrderChange.getId());
    //                     orderchangeMap.put("auditor", paramMap.get("auditor"));
    //                     orderchangeMap.put("audit_time", new Date());
    //                     //更新core_merchant_order表的数据
    //                     orderChantMap.put("source_protest", null);
    //                     orderChantMap.put("source_protest_charge", null);
    //                     orderChantMap.put("source_refund", exception_amount);
    //                     orderChantMap.put("source_refund_charge", source_protest_charge);
    //                     orderChantMap.put("order_id", paramMap.get("order_id"));
    //                     //更新core_merchant_order_rmb表的数据
    //                     orderChantRMBMap.put("source_rmb_protest", null);
    //                     orderChantRMBMap.put("source_rmb_protest_charge", null);
    //                     orderChantRMBMap.put("source_rmb_refund", change_rate_rmb);
    //                     orderChantRMBMap.put("source_rmb_refund_charge", fee_rmb);
    //                     orderChantRMBMap.put("order_id", paramMap.get("order_id"));
    //                     //更新channel_order_info表的数据
    //                     orderInfoMap.put("channel_refund_amount", change_rate_rmb_channel);
    //                     orderInfoMap.put("channel_protest_amount", null);
    //                     orderInfoMap.put("order_id", paramMap.get("order_id"));
    //                     //更新finance_business_money表的数据
    //                     orderBusinessMoneyMap.put("refund_amount", refund_amount.add(new BigDecimal(orderChantRMBMap.get("source_rmb_refund").toString())));
    //                     orderBusinessMoneyMap.put("usable_amount", usableAmount.subtract(new BigDecimal(orderBusinessMoneyMap.get("refund_amount").toString())));
    //                     orderBusinessMoneyMap.put("protest_amount", null);
    //                     orderBusinessMoneyMap.put("protest_charge_amount", null);
    //                     orderBusinessMoneyMap.put("ref_id", paramMap.get("mer_id"));
    //                     coreOrderMapper.updateOrder(orderMap);
    //                     coreOrderChangeMapper.getAbnormalAudit(orderchangeMap);
    //                     coreMerchantOrderMapper.updatecoreOrder(orderChantMap);
    //                     coreMerchantOrderRmbMapper.updatecoreOrderRMB(orderChantRMBMap);
    //                     channelOrderInfoMapper.updateorderInfoProtest(orderInfoMap);
    //                     financeBusinessMoneyMapper.updateFinanceBusinessMoney(orderBusinessMoneyMap);
    //                     msg = "1";
    //                     break;
    //                 } else if ("2".equals(msg)) {
    //                     orderchangeMap.put("status", 2);
    //                     orderchangeMap.put("remark", paramMap.get("remark"));
    //                     orderchangeMap.put("order_id", coreOrderChange.getId());
    //                     orderchangeMap.put("auditor", paramMap.get("auditor"));
    //                     orderchangeMap.put("audit_time", new Date());
    //                     coreOrderMapper.updateOrder(orderMap);
    //                     coreOrderChangeMapper.getAbnormalAudit(orderchangeMap);
    //                     msg = "2";
    //                 } else if ("3".equals(msg)) {
    //                     orderchangeMap.put("auditor", null);
    //                     orderchangeMap.put("audit_time", null);
    //                     orderchangeMap.put("status", 3);
    //                     orderchangeMap.put("order_id", coreOrderChange.getId());
    //                     coreOrderMapper.updateOrder(orderMap);
    //                     coreOrderChangeMapper.getAbnormalAudit(orderchangeMap);
    //                     msg = "3";
    //                 } else if ("4".equals(msg)) {
    //                     orderMap.put("exception_time", null);
    //                     orderMap.put("exception_status", 7);
    //                     orderMap.put("exception_amount", null);
    //                     orderMap.put("order_id", paramMap.get("order_id"));
    //                     orderchangeMap.put("auditor", null);
    //                     orderchangeMap.put("audit_time", null);
    //                     orderchangeMap.put("status", 3);
    //                     orderchangeMap.put("order_id", coreOrderChange.getId());
    //                     orderBusinessMoneyMap.put("refund_amount", refund_amount.add(new BigDecimal(orderChantRMBMap.get("source_rmb_refund").toString())));
    //                     orderBusinessMoneyMap.put("usable_amount", usableAmount.subtract(new BigDecimal(orderBusinessMoneyMap.get("refund_amount").toString())));
    //                     orderBusinessMoneyMap.put("protest_amount", null);
    //                     orderBusinessMoneyMap.put("protest_charge_amount", null);
    //                     orderBusinessMoneyMap.put("ref_id", paramMap.get("mer_id"));
    //                     coreOrderMapper.updateOrder(orderMap);
    //                     coreOrderChangeMapper.getAbnormalAudit(orderchangeMap);
    //                     financeBusinessMoneyMapper.updateFinanceBusinessMoney(orderBusinessMoneyMap);
    //                     msg = "4";
    //                 }
    //                 break;
    //             //全部拒付
    //             case 4:
    //                 orderchangeMap.put("status", paramMap.get("status"));
    //                 orderchangeMap.put("remark", paramMap.get("remark"));
    //                 orderchangeMap.put("order_id", coreOrderChange.getId());
    //                 orderchangeMap.put("auditor", paramMap.get("auditor"));
    //                 orderchangeMap.put("audit_time", new Date());
    //                 //更新core_order的表的数据
    //                 orderMap.put("exception_time", new Date());
    //                 orderMap.put("exception_status", 4);
    //                 orderMap.put("exception_amount", paramMap.get("exception_amount"));
    //                 orderMap.put("order_id", paramMap.get("order_id"));
    //                 //更新core_merchant_order表的数据
    //                 orderChantMap.put("source_refund", null);
    //                 orderChantMap.put("source_refund_charge", null);
    //                 orderChantMap.put("source_protest", paramMap.get("exception_amount"));
    //                 orderChantMap.put("source_protest_charge", source_protest_charge);
    //                 orderChantMap.put("order_id", paramMap.get("order_id"));
    //                 //更新core_merchant_order_rmb表的数据
    //                 orderChantRMBMap.put("source_rmb_refund", null);
    //                 orderChantRMBMap.put("source_rmb_refund_charge", null);
    //                 orderChantRMBMap.put("source_rmb_protest", new BigDecimal(paramMap.get("exception_amount").toString()).multiply(new BigDecimal(paramMap.get("change_rate").toString())));
    //                 orderChantRMBMap.put("source_rmb_protest_charge", new BigDecimal(paramMap.get("source_protest_charge").toString()).multiply(new BigDecimal(paramMap.get("change_rate").toString())));
    //                 orderChantRMBMap.put("order_id", paramMap.get("order_id"));
    //                 //更新channel_order_info表的数据
    //                 orderInfoMap.put("channel_refund_amount", null);
    //                 orderInfoMap.put("channel_protest_amount", new BigDecimal(paramMap.get("exception_amount").toString()).multiply(change_rate_channel));
    //                 orderInfoMap.put("order_id", paramMap.get("order_id"));
    //                 //更新finance_business_money表的数据
    //                 orderBusinessMoneyMap.put("refund_amount", null);
    //                 orderBusinessMoneyMap.put("usable_amount", usableAmount.subtract(protest_charge_amount_total).subtract(protest_amount_total));
    //                 orderBusinessMoneyMap.put("protest_amount", protest_amount.add(new BigDecimal(orderChantRMBMap.get("source_rmb_protest").toString())));
    //                 orderBusinessMoneyMap.put("protest_charge_amount", protest_charge_amount.add(new BigDecimal(orderChantRMBMap.get("source_rmb_protest_charge").toString())));
    //                 orderBusinessMoneyMap.put("ref_id", paramMap.get("mer_id"));
    //                 coreOrderMapper.updateOrder(orderMap);
    //                 coreOrderChangeMapper.getAbnormalAudit(orderchangeMap);
    //                 coreMerchantOrderMapper.updatecoreOrder(orderChantMap);
    //                 coreMerchantOrderRmbMapper.updatecoreOrderRMB(orderChantRMBMap);
    //                 channelOrderInfoMapper.updateorderInfoProtest(orderInfoMap);
    //                 financeBusinessMoneyMapper.updateFinanceBusinessMoney(orderBusinessMoneyMap);
    //                 msg = "1";
    //                 break;
    //             //部分拒付
    //             case 5:
    //                 orderchangeMap.put("status", paramMap.get("status"));
    //                 orderchangeMap.put("remark", paramMap.get("remark"));
    //                 orderchangeMap.put("order_id", coreOrderChange.getId());
    //                 orderchangeMap.put("auditor", paramMap.get("auditor"));
    //                 orderchangeMap.put("audit_time", new Date());
    //                 //更新core_order的exception_time,exception_status,exception_amount,order_id
    //                 orderMap.put("exception_time", new Date());
    //                 orderMap.put("exception_status", 5);
    //                 orderMap.put("exception_amount", exception_amount);
    //                 orderMap.put("order_id", paramMap.get("order_id"));
    //                 //更新core_merchant_order
    //                 orderChantMap.put("source_refund", null);
    //                 orderChantMap.put("source_refund_charge", null);
    //                 orderChantMap.put("source_protest", exception_amount);
    //                 orderChantMap.put("source_protest_charge", source_protest_charge);
    //                 orderChantMap.put("order_id", paramMap.get("order_id"));
    //                 //更新core_merchant_order_rmb
    //                 orderChantRMBMap.put("source_rmb_refund", null);
    //                 orderChantRMBMap.put("source_rmb_refund_charge", null);
    //                 orderChantRMBMap.put("source_rmb_protest", change_rate_rmb);
    //                 orderChantRMBMap.put("source_rmb_protest_charge", fee_rmb);
    //                 orderChantRMBMap.put("order_id", paramMap.get("order_id"));
    //                 //更新channel_order_info
    //                 orderInfoMap.put("channel_refund_amount", null);
    //                 orderInfoMap.put("channel_protest_amount", change_rate_rmb_channel);
    //                 orderInfoMap.put("order_id", paramMap.get("order_id"));
    //                 //更新finance_business_money
    //                 orderBusinessMoneyMap.put("refund_amount", null);
    //                 orderBusinessMoneyMap.put("usable_amount", usableAmountTotal);
    //                 orderBusinessMoneyMap.put("protest_amount", protest_amount_total);
    //                 orderBusinessMoneyMap.put("protest_charge_amount", protest_charge_amount_total);
    //                 orderBusinessMoneyMap.put("ref_id", paramMap.get("mer_id"));
    //                 coreOrderMapper.updateOrder(orderMap);
    //                 coreOrderChangeMapper.getAbnormalAudit(orderchangeMap);
    //                 coreMerchantOrderMapper.updatecoreOrder(orderChantMap);
    //                 coreMerchantOrderRmbMapper.updatecoreOrderRMB(orderChantRMBMap);
    //                 channelOrderInfoMapper.updateorderInfoProtest(orderInfoMap);
    //                 financeBusinessMoneyMapper.updateFinanceBusinessMoney(orderBusinessMoneyMap);
    //                 msg = "1";
    //                 break;
    //             //代理拒付
    //             case 6:
    //                 orderMap.put("exception_time", new Date());
    //                 orderMap.put("exception_status", 6);
    //                 orderMap.put("order_id", paramMap.get("order_id"));
    //                 break;
    //             default:
    //                 break;
    //         }
    //     } else {
    //         orderchangeMap.put("status", paramMap.get("status"));
    //         orderchangeMap.put("remark", paramMap.get("remark"));
    //         orderchangeMap.put("order_id", coreOrderChange.getId());
    //         orderchangeMap.put("auditor", paramMap.get("auditor"));
    //         orderchangeMap.put("audit_time", new Date());
    //         coreOrderChangeMapper.getAbnormalAudit(orderchangeMap);
    //         msg = "1";
    //     }
    //     return msg;
    // }




    /**
     * 发送银行
     */
    // public String refund(CoreOrderChange coreOrderChange, CoreOrder coreOrder) throws IOException {
    //     String msg;
    //     Map<String, Object> param = new HashMap<>();
    //     param.put("orderChange", JsonUtils.objectToJson(coreOrderChange));
    //     param.put("order", JsonUtils.objectToJson(coreOrder));
    //     param.put("sign", DigestUtils.md5Hex(coreOrderChange.getOrderId() + "DkUlMgBj+Cj!+mT" + coreOrderChange.getCurrency() + coreOrderChange.getAmount()));
    //     CoreAcquirer coreAcquirer = coreAcquirerMapper.selectByPrimaryKey(acquirerAccountMapper.selectByPrimaryKey(Long.parseLong(coreOrder.getAccountId().toString())).getAcquirerId());
    //     String url = StringUtils.isEmpty(coreAcquirer.getRemark()) ? "http://192.168.1.23:8010/payment/" + REFUND_URL : coreAcquirer.getRemark();
    //     HttpClientUtils.setTIMEOUT(50000);
    //     Result result = JsonUtils.jsonToPojo(HttpClientUtils.doPost(HttpClientUtils.getHttpClient(), url, param, "UTF-8"), Result.class);
    //     if (result != null) {
    //         switch (result.getCode()) {
    //             //异常操作
    //             case 0:
    //                 return "0";
    //             //银行通过退款
    //             case 1:
    //                 return "1";
    //             //银行拒绝退款
    //             case 2:
    //                 return "2";
    //             //银行超时请求
    //             case 3:
    //                 coreOrderChange.setRemark(result.getMsg());
    //                 coreOrderChangeMapper.updateByPrimaryKeySelective(coreOrderChange);
    //                 return "3";
    //             //退款申请成功，等待处理
    //             case 4:
    //                 return "4";
    //             default:
    //                 break;
    //         }
    //         return String.valueOf(result.getCode());
    //     }
    //     return "3";
    // }

    /**
     * 获取提现审核数据
     */
    @Override
    public List<Map<String, Object>> getWithdrawal(Map<String, Object> paramMap) {
        Map<String, Object> resultMap = new HashMap<>();
        List<Map<String, Object>> list = financeDrawingMapper.getWithdrawal(paramMap);
        int result = financeDrawingMapper.getWithdrawalCheckNum(paramMap);
        resultMap.put("result", result);
        list.add(resultMap);
        return list;
    }

    /**
     * 查询提现审核数据
     */
    @Override
    public List<Map<String, Object>> getWithdrawalCheck(Map<String, Object> paramMap) {
        return financeDrawingMapper.getWithdrawalCheck(paramMap);
    }

    /**
     * 审核提现审核数据
     */

    @Override
    public Boolean updateWithdrawalAudit(Map<String, Object> paramMap) {

        int status = Integer.parseInt(paramMap.get("status").toString());

        if (SystemConstant.FINANCE_DRAWING_STATUS_PENDING_PAYMENT != status && SystemConstant.FINANCE_DRAWING_STATUS_PENDING_REVIEW != status) {

            Map<String, Object> transferMap = financeBusinessMoneyMapper.getUsableAmount(Integer.parseInt(paramMap.get("refId").toString()));
            BigDecimal audit_amount = ((BigDecimal) transferMap.get("audit_amount")).subtract(new BigDecimal(paramMap.get("amount").toString()));    //待审核金额-提现金额

            Map<String, Object> financeMap = new HashMap<>();
            financeMap.put("audit_amount", audit_amount);
            financeMap.put("ref_id", paramMap.get("ref_id"));

            switch (status) {

                case 3:  //划款

                    BigDecimal book_balance = ((BigDecimal) transferMap.get("book_balance")).subtract(new BigDecimal(paramMap.get("amount").toString()));   //账户余额-提现金额
                    BigDecimal success_amount = ((BigDecimal) transferMap.get("success_amount")).add(new BigDecimal(paramMap.get("amount").toString()));  //成功提现金额+提现金额
                    financeMap.put("book_balance", book_balance);
                    financeMap.put("success_amount", success_amount);
                    paramMap.put("transfer", paramMap.get("currentUser"));
                    paramMap.put("transfer_time", new Date());
                    break;

                case 0:  //无效

                    BigDecimal usable_amount = ((BigDecimal) transferMap.get("usable_amount")).add(new BigDecimal(paramMap.get("amount").toString()));    //可用余额+提现金额
                    financeMap.put("usable_amount", usable_amount);
                    break;
            }

            financeBusinessMoneyMapper.updateByFinanceBusinessMoney(financeMap);

        } else {

            paramMap.put("auditor", paramMap.get("currentUser"));
            paramMap.put("audit_time", new Date());


        }


        return financeDrawingMapper.getWithdrawalAudit(paramMap);
    }

    @Override
    public List<Map<String, Object>> getreviewPrintData(Map<String, Object> paramMap) {
        paramMap.put("status", SystemConstant.FINANCE_DRAWING_STATUS_PENDING_REVIEW); //只打印待复核的数据
        return financeDrawingMapper.queryReviewPrintData(paramMap);
    }


}
